import CONFIG from './config.js'
import Message from './message.js'

class LevelManager {
    constructor(game) {
        this.game = game;
        this._status = "finish"; // prepare, running, finish
        this.ts = new Date().getTime();
        this.level = 0;
    }

    handle_level() {
        if (this.is_prepare) {
            let ts = new Date().getTime();
            if (ts - this.ts >= CONFIG.level_pause_time * 1000) {
                this.status = "running";
                this.spirit_count = this.game.game_info.spirit_count;
            }
        }
        if (this.is_running) {
            let spirit_count = this.game.game_info.spirit_count - this.spirit_count;
            let spirit_config = this.game.config.spirit;
            if (spirit_count >= spirit_config.max_live_spirit * spirit_config.spirit_batch) {
                this.status = "finish";
            }
        }
        if (this.is_finish) {
            if (this.game.game_info.spirit_list.length === 0) {
                this.ts = new Date().getTime();
                this.upgrade();

                let msg = {text: "第 " + this.level + " 关", size: 6, duration: 2};
                Message.add_message(msg);

                for (let index = 0; index < CONFIG.level_pause_time - 2; index++) {
                    let delay = 2 + index;
                    let text = CONFIG.level_pause_time - 2 - index;

                    let msg = {text: text, size: 10, duration: 1, delay: delay};
                    Message.add_message(msg);
                }

                this.status = "prepare";
            }
        }
    }

    upgrade() {
        this.level++;
        if (this.level === 1) return;

        let spirit_config = this.game.config.spirit;
        spirit_config.max_live_spirit = spirit_config.max_live_spirit * 1.2 | 0;
        spirit_config.gen_interval = spirit_config.gen_interval / 1.1;
        spirit_config.spirit_batch += 1;

        let spirit_types = this.game.config.spirit_types;
        spirit_types.forEach(function (config) {
            config.move_interval = config.move_interval / 1.2 | 0;
            config.blood = config.blood * 1.2 | 0;
        });
    }

    get is_prepare() {
        return this._status === "prepare";
    }

    get is_running() {
        return this._status === "running";
    }

    get is_finish() {
        return this._status === "finish";
    }

    set status(s) {
        this._status = s;
    }

    // get status(){
    //     return this._status;
    // }
}

export default LevelManager;